{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3880e38c-7d6a-49aa-bdb0-94cd6c489b14",
   "metadata": {},
   "source": [
    "# 操作PPT\n",
    "\n",
    "- python-pptx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f2334ab1-4eb6-413a-9f78-2960d3465581",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pptx import Presentation\n",
    "\n",
    "# 创建幻灯片对象\n",
    "pres = Presentation()\n",
    "\n",
    "# 选择母版添加一页\n",
    "title_slide_layout = pres.slide_layouts[0]\n",
    "slide = pres.slides.add_slide(title_slide_layout)\n",
    "# 获取标题栏和副标题栏\n",
    "title = slide.shapes.title\n",
    "subtitle = slide.placeholders[1]\n",
    "# 编辑标题和副标题\n",
    "title.text = \"Welcome to Python\"\n",
    "subtitle.text = \"Life is short, I use Python\"\n",
    "\n",
    "# 选择母版添加一页\n",
    "bullet_slide_layout = pres.slide_layouts[1]\n",
    "slide = pres.slides.add_slide(bullet_slide_layout)\n",
    "# 获取页面上所有形状\n",
    "shapes = slide.shapes\n",
    "# 获取标题和主体\n",
    "title_shape = shapes.title\n",
    "body_shape = shapes.placeholders[1]\n",
    "# 编辑标题\n",
    "title_shape.text = 'Introduction'\n",
    "# 编辑主体内容\n",
    "tf = body_shape.text_frame\n",
    "tf.text = 'History of Python'\n",
    "# 添加一个一级段落\n",
    "p = tf.add_paragraph()\n",
    "p.text = 'X\\'max 1989'\n",
    "p.level = 1\n",
    "# 添加一个二级段落\n",
    "p = tf.add_paragraph()\n",
    "p.text = 'Guido began to write interpreter for Python.'\n",
    "p.level = 2\n",
    "\n",
    "# 保存幻灯片\n",
    "pres.save('ppt/test.pptx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "741b80f1-97c3-4617-96d6-f427587a2cb9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
